const Service = require("uni-cloud-router").Service;
module.exports = class signAuditService extends Service{
	async pmUploadCheckResult({_id,signinStatus,projectID,drillDeep,wkID,holeNum}){
		const dbCmd = this.db.command;
		
		let res = await this.db.collection('signin_store').doc(_id).update({
			signinStatus
		})
		
		// 增加一天打卡 && 增加孔深
		let countAddRes,wkCountAddRes,editHoleRes;
		if(signinStatus === 1) {
			//每个员工打卡 给项目添加孔深和天数
			countAddRes = await this.db.collection('project_store').where({
				_id:projectID
			}).update({
				signCountDay:dbCmd.inc(1),
				drillDeepCount:dbCmd.inc(drillDeep)
			})
			
			//给当前员工添加 孔深和天数
			wkCountAddRes = await this.db.collection("project_store").where({
				_id:projectID,
				"worker_ids.wkID":wkID
			}).update({
				"worker_ids.$.drillDeepCount":drillDeep,
				"worker_ids.$.signCountDay":dbCmd.inc(1)
			})
			
			//给当前孔号添加深度
			editHoleRes = await this.db.collection('project_store').where({
				_id:projectID,
				"holes.num":holeNum
			}).update({
				"holes.$.deep":drillDeep
			})
		}
		
		//审核拒绝
		if(signinStatus === -1 && res.affectedDocs !== 0){
			return {
				status:0,
				msg:"操作成功，已经拒绝"
			}
		}
		
		if(res.affectedDocs !== 0 && countAddRes.affectedDocs !== 0 && wkCountAddRes.affectedDocs !== 0 && editHoleRes.affectedDocs !== 0){
			return {
				status:0,
				msg:"操作成功"
			}
		} else {
			return {
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async getSignInof({projectID,pageNo,pageSize,signinStatus}){
		let res = await this.db.collection('signin_store')
		.aggregate()
		.match({
			projectID,
			signinStatus
		})
		.skip((pageNo-1)*pageSize)
		.limit(pageSize)
		.end()
		
		if(res.affectedDocs !== 0 || res.data.length !== 0){
			return {
				status:0,
				msg:"成功",
				data:res.data
			}
		} else {
			return {
				status:1,
				msg:"没有数据"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
}